package com.google.shop.product.wxlogin;

import com.alibaba.fastjson.JSONObject;
import com.google.shop.product.common.utils.JwtUtils;
import com.google.shop.product.entity.member.BuyerInfo;
import com.google.shop.product.service.member.BuyerInfoService;
import com.google.shop.product.vo.member.BuyerInfoVO;
import com.google.shop.product.wxlogin.entity.TextMessage;
import com.thoughtworks.xstream.XStream;
import io.opentracing.log.Fields;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.MessageHeaders;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"wx"})
@RestController
@CrossOrigin
/* loaded from: app.jar:BOOT-INF/classes/com/google/shop/product/wxlogin/Controller.class */
public class Controller {
    @Autowired
    WxService service;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    BuyerInfoService buyerInfoService;

    @RequestMapping({"getMsg"})
    String wx(HttpServletRequest request) throws Exception {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter(MessageHeaders.TIMESTAMP);
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        System.out.println(signature);
        System.out.println(timestamp);
        System.out.println(nonce);
        System.out.println(echostr);
        if (echostr != null) {
            System.out.println("如果echostr不是空，说明是接口配置验证");
            return echostr;
        }
        Map<String, String> map = this.service.parseXml(request);
        System.out.println("wx服务器发来的消息：" + map);
        map.get("MsgType");
        TextMessage textMessage = new TextMessage(map, "o");
        XStream xStream = new XStream();
        xStream.processAnnotations(TextMessage.class);
        String xml = xStream.toXML(textMessage);
        String event = map.get("Event");
        String ticket = map.get("Ticket");
        if (event != null && (event.equals("SCAN") || event.equals("subscribe"))) {
            this.stringRedisTemplate.opsForValue().set("wx_login_ticket:" + ticket, map.get("FromUserName"), 1000, TimeUnit.SECONDS);
        }
        if (map.get("MsgType").equals(Fields.EVENT)) {
            return echostr;
        }
        return xml;
    }

    @RequestMapping({"getQRcode"})
    @CrossOrigin
    Map getQRcode() throws Exception {
        Map map = this.service.getQRCode();
        map.get("url").toString();
        this.stringRedisTemplate.opsForValue().set("wx_login_ticket:" + map.get("ticket").toString(), "noscan", 1000, TimeUnit.SECONDS);
        return map;
    }

    @RequestMapping({"isScan"})
    @CrossOrigin
    Map isScan(String ticket) throws Exception {
        String redisValue = this.stringRedisTemplate.opsForValue().get("wx_login_ticket:" + ticket);
        HashMap<Object, Object> map = new HashMap<>();
        if (redisValue == null) {
            map.put("code", 2);
            map.put("msg", "二维码过期");
        } else if (!redisValue.equals("noscan")) {
            map.put("code", 0);
            map.put("msg", "扫码ok，转跳主页");
            map.put("ticket", ticket);
            map.put("wxid", redisValue);
            BuyerInfo redisValue1 = this.buyerInfoService.findByBuyerEmail(redisValue);
            if (redisValue1 == null) {
                this.buyerInfoService.saveBuyerInfo(new BuyerInfoVO(redisValue, "abc123456789"));
                redisValue1 = this.buyerInfoService.findByBuyerEmail(redisValue);
            }
            map.put("jwtToken", JwtUtils.getJwtToken(redisValue1.getBuyerId() + "", getUserInfo(redisValue).get("nickname") + ""));
        } else {
            map.put("code", 1);
            map.put("msg", "没有扫码");
            map.put("ticket", ticket);
        }
        return map;
    }

    @GetMapping({ "getUserInfo" })
    Map getUserInfo(final String openid) throws Exception {
        final String accessToken = this.service.getToken();
        Map jsonToMap = null;
        try {
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;
            try {
                final HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN");
                client = HttpClients.createDefault();
                response = client.execute((HttpUriRequest)httpGet);
                final HttpEntity entity = response.getEntity();
                final String result = EntityUtils.toString(entity);
                jsonToMap = (Map) JSONObject.parseObject(result);
            }
            finally {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return jsonToMap;
    }
}
